import { put, takeEvery } from 'redux-saga/effects'
// import { actionTypes } from '../common/Header/store'
import { headerActionTypes } from '../common/Header/store'
import { homeActionTypes } from '../pages/Home/store'
import { fromJS } from 'immutable'

import * as Api from '../Api/index'

function* fetchSearchList () {
  try {
    const res = yield Api.getSearchList()
    yield put({ type: headerActionTypes.INIT_SEARCH_LIST, value: fromJS(res.data.array) })
  } catch (e) {
    console.log('error', e)
  }
}

function* fetchArticleList () {
  try {
    const res = yield Api.getArticleList()
    yield put({ type: homeActionTypes.FETCH_ARTICLE_SUCCESS, payload: fromJS(res.data.data) })
  } catch (e) {
    console.log(e)
  }
}

function* fetchTopicList() {
  try {
    const res = yield Api.getTopicList()
    yield put({ type: homeActionTypes.FETCH_TOPIC_SUCCESS, payload: fromJS(res.data.data) })
  } catch (e) {
    console.log(e)
  }
}

function* mySaga () {
  yield takeEvery(headerActionTypes.GET_SEARCH_LIST, fetchSearchList)
  yield takeEvery(homeActionTypes.FETCH_ARTICLE, fetchArticleList)
  yield takeEvery(homeActionTypes.FETCH_TOPIC, fetchTopicList)
}

export default mySaga
